<template>
  <div>
 <div class="dialogPop">
      <el-dialog :visible.sync="addData.dialogAdd"  title="添加赠送对象" 
      :before-close="handleClose" size="tiny">
        <el-form :model="addData" @submit.native.prevent
         ref="addData">
                <el-row :gutter="20">
                   <el-col>
                        <el-form-item >
                            <el-input
                                placeholder="请输入e学云ID号进行搜索"
                                icon="search"
                                v-model="eCode"
                                :on-icon-click="handleIconClick" :maxlength="10">
                                </el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
        </el-form>
        <div class="search-result" v-show="AnnounceList.length!=0">
             <p>搜索结果:</p>
             <div>
                <el-table :data="AnnounceList" style="width: 100%" ref="multipleTable" 
                @selection-change="handleSelectionChange" border>
                        <el-table-column
                            type="selection"
                            width="55">
                        </el-table-column>
                        <el-table-column align="center" label="e学云ID号" prop="eCode" >
                        </el-table-column>
                        <el-table-column align="center" label="地区"  prop="adress" >
                        </el-table-column>
                        <el-table-column align="center" label="姓名"  prop="name">
                        </el-table-column>
                </el-table>
             </div>
             <!-- <div class="error-find" v-show="errorFind">没有找到该用户，再核对一下呢？</div> -->
        </div>
        <div slot="footer" class="dialog-footer">
          <p class="btn">
            <el-button type="primary" @click="addInfor('addData')" 
            :loading="loadingBtn" :disabled="multipleSelection.length==0">添 加</el-button>
          </p>
        </div>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import {judgeByeCode, addUserByeCode} from 'api/creditShop';
export default {
    props: {
        addData: {
            default() {
                return {
                    dialogAdd: false // 默认不显示
                }
            },
            required: true
        }
    },
    data() {
        return {
            eCode: '',
            // errorFind: false,
            AnnounceList: [],
            addDataList: [],
            multipleSelection: [],
            loadingSearch: false,
            addDateBtn: true,
            loadingBtn: false // 确定按钮的 loading
        }
    },
    methods: {
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        // 搜索按钮
        handleIconClick() {
            let eCode=this.eCode;
            var patrn = /^\d{5,10}$/g;
            if (!eCode) {
                this.$message.info('请输入e学云ID号')
            } else {
                if (!patrn.test(eCode)) {
                    this.$message.info('请输入合法的e学云ID号')
                } else {
                    judgeByeCode(eCode).then(res => {
                        let infor=res.data.content;
                        this.AnnounceList=[// 弹窗的列表
                            {
                                eCode: infor.eCode?infor.eCode:'--',
                                adress: infor.adress?infor.adress:'--',
                                name: infor.name?infor.name:'--'
                            }
                        ]
                    }).catch(() => {
                        // this.errorFind=true;// 提示没有找到该用户，请核对
                        this.AnnounceList=[];
                    })
                }

            } 
        },
         // 弹窗的添加按钮 添加赠送对象
        addInfor(addData) {
            this.$refs[addData].validate((valid) => {
                if (valid) {// 验证通过
                    if (this.addData.AnnounceList.length>50) {
                        this.$message.error('一次最多添加赠送人数50人')
                    } else {
                        let eCode=this.eCode;
                        this.addUserByeCode(eCode)// 单个新增
                    }
                } 
            });
        },
           // 单个新增
        addUserByeCode(eCode) {
            let list=this.addData.AnnounceList;
            addUserByeCode(eCode, list).then(res => {
                this.addData.AnnounceList=res.data.content;// 添加的返回列表 传给父组件显示出来
                this.$message.success('添加赠送对象成功！')
                this.delDialogAdd();
            });

        },
      
        // 弹出框的关闭按钮
        handleClose(done) {
            this.$refs.addData.resetFields();
            this.delDialogAdd();
        },
        // 还原弹窗信息
        delDialogAdd() {
            this.addData.dialogAdd = false;
            this.eCode='';
            this.AnnounceList=[];
        }
    }
}
</script>

<style lang="scss" scoped>
.dialog-footer{
    text-align: center;
}
.error-find{
    color:#999;
    height: 90px;
    line-height: 90px;
    text-align: center;
}
</style>

